#include "apue.h"
#include <sys/wait.h>

int main(void) {
    pid_t pid;
    if ((pid =fork()) < 0) 
        err_sys("fork failed");
    if (pid == 0) {
        printf("(pid %d, ppid %d)child will exit after 5 second.\n", getpid(), getppid()); 
        sleep(5);
        printf("(pid %d, ppid %d)child exited.\n", getpid(), getppid()); 
        exit(0);
    }

    printf("(pid %d) parent will not get child state after 20 second\n", getpid());
    sleep(20);
    if (waitpid(pid, NULL, 0) != pid)
        err_sys("wait error");
    
    printf("(pid %d) parent will exit after 20 second\n", getpid());
    sleep(20);
    printf("parent exited.");
    return 0;
}
